package com.shop.trade.atom.atomsvc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.trade.OrderPayInput;
import com.shop.common.model.result.trade.OrderPayOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.trade.api.model.StatisModel;
import com.shop.trade.atom.atomsvc.IPayAtomSvc;
import com.shop.trade.atom.dao.OrderPayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PayAtom extends BaseService implements IPayAtomSvc {

    @Autowired
    OrderPayMapper orderPayMapper;

    @Override
    public ServiceBean<Integer> insertOrderPay(OrderPayInput orderPayInput) {
        orderPayInput.setBalance(orderPayInput.getBalance()==null?new BigDecimal(0):orderPayInput.getBalance());
        orderPayInput.setPayPrice(orderPayInput.getPayPrice()==null?new BigDecimal(0):orderPayInput.getPayPrice());
        int i = orderPayMapper.insertSelective(orderPayInput);
        if (opSuccess(i)) {
            return resultData(orderPayInput.getId());
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<Integer> deleteOrderPay(Integer id) {
        int i = orderPayMapper.deleteByPrimaryKey(id);
        if (opSuccess(i)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<OrderPayOutput> selectOrderPayById(Integer id) {
        OrderPayOutput orderPayOutput = orderPayMapper.selectByPrimaryKey(id);
        if (orderPayOutput != null) {
            return resultData(orderPayOutput);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<OrderPayOutput> qryOrderPay(OrderPayInput orderPayInput) {
        List<OrderPayOutput> list = orderPayMapper.qryOrderPays(orderPayInput);
        if (list != null && list.size() > 0) {
            return resultData(list.get(0));
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<OrderPayOutput>> selectOrderPayList(OrderPayInput orderPayInput) {
        List<OrderPayOutput> list = orderPayMapper.qryOrderPays(orderPayInput);
        if (list != null && list.size() > 0) {
            return resultData(list);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<OrderPayOutput>> selectOrderPayList4P(InputBean<OrderPayInput> inputBean) {
        PageHelper.startPage(inputBean.getIndex(), inputBean.getPageSize());
        List<OrderPayOutput> list = orderPayMapper.qryOrderPays(inputBean.getParam());
        ServiceBean<List<OrderPayOutput>> serviceBean = new ServiceBean<>();
        if (list != null && list.size() > 0) {
            PageInfo<OrderPayOutput> pageInfo = new PageInfo<>(list);
            serviceBean.setSuccess(true);
            serviceBean.setData(pageInfo.getList());
            serviceBean.setMaxSize(pageInfo.getTotal());
        } else {
            serviceBean.setSuccess(false);
        }
        return serviceBean;
    }

    @Override
    public ServiceBean<Integer> updateOrderPay(OrderPayInput orderPayInput) {
        int i = orderPayMapper.updateByPrimaryKeySelective(orderPayInput);
        if (opSuccess(i)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public List<StatisModel> qryStatis(StatisModel statisModel) {
        return orderPayMapper.qryStatis(statisModel);
    }
}
